package top.hzy520.valuation.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.hzy520.common.resp.ApiResult;
import top.hzy520.valuation.entity.ProductSku;
import top.hzy520.valuation.entity.SalesItem;
import top.hzy520.valuation.feign.SalesServiceClient;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 20:15 2018/6/26
 */
@Service
public class ValuationServiceImpl implements ValuationService{
     @Autowired
     private SalesServiceClient salesServiceClient;
     /**
     *@Author: HouZeYu
     *@Description: 计算商品的价格
     *@Date: 21:14 2018/6/27
     */
    @Override
    public BigDecimal valuationGoodsPrice(ProductSku productSku) {
        ApiResult<List<SalesItem>> result=salesServiceClient.findProductSkuSales(productSku);
        List<SalesItem> salesItemList=result.getData();
        salesItemList.stream().forEach(param->{
            //判断商品是否存在活动价格
            if (productSku.getActivityPrice()==null){
                productSku.setSkuPrice(productSku.getSkuPrice().subtract(new BigDecimal(param.getSaleRuleDetail())));
            }else {
                //减去活动的减价
                productSku.setActivityPrice(productSku.getActivityPrice().subtract(new BigDecimal(param.getSaleRuleDetail())));
            }
        });
            if (productSku.getActivityPrice()==null){
                return productSku.getSkuPrice();
            }
            return productSku.getActivityPrice();
    }
}
